package com.example.movie.data.user.model

import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.forward.androids.utils.ThreadUtil
import com.example.movie.R
import com.example.movie.common.constants.Constants
import com.example.movie.common.constants.LoadingState
import com.example.movie.common.model.RequestResult
import com.example.movie.common.model.Result
import com.example.movie.data.movie.model.vo.SearchVo
import com.example.movie.utils.VideoBean

class UserInfoPageViewModel(private val repository: UserRepository) : ViewModel() {

    private val _hopeLookList = MutableLiveData<RequestResult<List<SearchVo>>>()
    val hopeLookList: MutableLiveData<RequestResult<List<SearchVo>>>
        get() = _hopeLookList

    private val _haveSeenList = MutableLiveData<RequestResult<List<SearchVo>>>()
    val haveSeenList: MutableLiveData<RequestResult<List<SearchVo>>>
        get() = _haveSeenList

    private val _videoList = MutableLiveData<List<VideoBean>>()
    val videoList:MutableLiveData<List<VideoBean>>
         get() = _videoList

//    private val _index = MutableLiveData<Int>()
//    val text: LiveData<String> = Transformations.map(_index) {
//        "Hello world from section: $it"
//    }
//
//    fun setIndex(index: Int) {
//        _index.value = index
//    }

    fun loadMyMovieList(type: Int) {
        var result: Result<List<SearchVo>>? = null;
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    if (result is Result.Success) {
                        if (type == Constants.UserInfo.HOPE_LOOK_LIST){
                            _hopeLookList.value = RequestResult(
                                success = (result as Result.Success<List<SearchVo>>).data
                            )
                        }else{
                            _haveSeenList.value = RequestResult(
                                success = (result as Result.Success<List<SearchVo>>).data
                            )
                        }

                    } else {
                        if (type == Constants.UserInfo.HOPE_LOOK_LIST){
                            _hopeLookList.value = RequestResult(
                                error = R.string.request_fail
                            )
                        }else{
                            _haveSeenList.value = RequestResult(
                                error = R.string.request_fail
                            )
                        }

                    }
                }
            }
        }

        ThreadUtil.getInstance().execute{
            result = repository.loadMyMovieList(type)
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }

    }

    fun loadMyLikeMovieVideoList() {
        var result: Result<List<VideoBean>>? = null;
        val handler = object : Handler(Looper.myLooper()!!) {
            override fun handleMessage(msg: Message) {
                if (msg.what == LoadingState.OK) {
                    if (result is Result.Success){
                        _videoList.value = (result as Result.Success<List<VideoBean>>).data
                    }
                }
            }
        }

        ThreadUtil.getInstance().execute{
            result = repository.loadMyLikeMovieVideoList()
            val message = Message()
            message.what = LoadingState.OK
            handler.sendMessage(message)
        }
    }


}